Page({
    data: {
        value : ''
    },

    onLoad: function (opt) {
        let that = this
        that.setData({
            name : opt.name ,
            ava : opt.ava ,
            id : opt.id
        })
    },

    xInput: function (e) {
        let that = this
        let value = e.detail.value
        that.setData({
            value: value
        })
    },

    xSub: function () {
        let that = this
        let value = that.data.value
        let id = that.data.id
        if(value === ''){
            wx.showToast({
                title: '请输入内容后提交',
                icon: 'none'
            })
        }else{
            wx.requestSubscribeMessage({
                tmplIds: [
                    "s-Mq0IcYoqL6yFi0ttPSTngYneBzFi6O7NKAVRWT0pM",
                    "TBmwFgapJW30qV3VfL517MUqLvbyftvQhNWD6rWideI"
                ],
                success: (res) => {
                    if (res['s-Mq0IcYoqL6yFi0ttPSTngYneBzFi6O7NKAVRWT0pM'] === 'accept') {
                        wx.cloud.callFunction({
                            name: 'message_subscribe',
                            data: {
                                action: "add_subscribe",
                                dataObj: {
                                    templateid: "s-Mq0IcYoqL6yFi0ttPSTngYneBzFi6O7NKAVRWT0pM",
                                    subscribe_status_string: "accept"
                                }
                            }
                        }).then(res => {
                            wx.showToast({
                                title: '订阅成功',
                                icon: 'none',
                                duration: 1000,
                            })
                            let _dataObj ={
                                to_openid: id,
                                message_type: 1, // 1:文字
                                business_type: 4, // 4:提问
                                content: {
                                  title: value, //标题
                                  content:{
                                       pic: "", //图片
                                       text: value, //文本
                                       audio: "", //语音
                                       parameter: "" //参数，根据业务类型和参数做对应的跳转
                                   }
                                }
                            }
                            wx.cloud.callFunction({
                                name: 'consulting_message',
                                data: {
                                    action: "send_message",
                                    dataObj: _dataObj
                                }
                            }).then(res => {
                                wx.showToast({
                                    title: '提交成功',
                                    icon: 'none',
                                    success : () => {
                                        setTimeout( () => {
                                            wx.navigateBack({
                                                delta: -1,
                                            })
                                        } , 1000)
                                    }
                                })
                            })
                        })
                    }
                },
                fail() { }
            })
        }
    }
})